<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;
use DB;
class AlpcController extends Controller
{

  public $app = null;
  public function __construct(){
    $options = [
      'debug'  => true,
      'app_id' => 'wx61a5ce5230a32e92',
      'secret' => '14ba21479bce9ff60ba93185cbc5e4bb',
      'token'  => 'johntest',
      // 'aes_key' => null, // 可选
      'log' => [
          'level' => 'debug',
          'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
      ],
      'oauth' => [
         'scopes'   => ['snsapi_base'],
         'callback' => '/allogin',
       ],
      'guzzle'=>[
        'timeout'=>5.0,
        'verify'=>false,
      ]
      //...
    ];
    $this->app = new Application($options);
  }
    public function allogin(Request $req){
        $oauth = $this->app->oauth;
        $user = $oauth->user();
        $req->session()->put('user', $user);
        //return redirect('/center');
	return $this->altiao($user);
    }
    public function alcenter(Request $req){
//return 1;exit;
            $oauth = $this->app->oauth;
            return $oauth->redirect();
            //return redirect('login');
    }

    public function allogout(Request $req){
        $req->session()->forget('user');
        return $this->redirect('/alcenter');
    }
    public function altiao($id){
	//return $this->https2_request();
        //return view('lijitiaozhuan',['id'=>$id]);
	$open = $id['id'];
	$userService = $this->app->user;
        $userer = $userService->get($open);
        $name = $userer->nickname;
        $sex = $userer->sex;//   1是男 2是女
	$url = "http://www.laoyou1.me/shhomer/output/GetGates.php?openid=$open";

        $did = $this->https2_request($url);
        $did = json_decode($did);
	//获取 did
        $dids= $did->Data[0][0];
	$name = $did->Data[0][1];
        $now = time();

	$url = "http://www.laoyou1.me/shhomer/output/version_index.php?openid=$open&did=$dids";
	$data = $this->https2_request($url);
	$data = json_decode($data);
	if( $data->Data ==3  )
	{
		return redirect("http://www.laoyou1.me/fire_wlj/shouye.php?openid=$open&did=$dids&gatename=$name&v=$now");
	}

	return redirect("http://www.laoyou1.me/chart1.3/index.php?openid=$open&did=$dids&gatename=$name&v=$now");
    }
    public function visit(){
	if( empty( $_POST )) {
		$openID = $_GET['openID'];
		$did = $_GET['did'];
		$name = $_GET['name']; // 谁家

	}else{
    		$openID = $_POST['openID'];
		$did = $_POST['did'];
		$name = $_POST['name']; // 谁家

	}
	if( empty($did) && ( !empty($name) ) ){
		exit;
	}
	$sub_did = substr( $did , -4 );
	$arr= ['2266' , '3744' , '5480' , '9173' ,'0663'];
	//$name = $_POST['name']; // 谁家
	$times = date( 'Y-m-d H:i:s' , time() );//时间
        $openID_name = DB::table('tbmember')->where('openID' , $openID)->first();  // 查询微信信息
        $gatemember = DB::table('tbgatemember')->where(['did'=>$did , 'openID'=>$openID])->first();
	$relation = $gatemember->relation;  // did  openID  关系 , 本人 , 管理员
	$ID_name = $openID_name->name; //openID 昵称
	$data = ['name'=>$name , 'openID'=>$openID , 'did'=>$did , 'timestmp'=>$times , 'openID_name'=>$ID_name , 'relation'=>$relation];
	//dd( $data );
	 DB::table('tbvisit')->insert($data );
	$now = time();
	$h = date( 'H' , $now );
	$apple=[];
	$apple[0] = 'o3eaus6PSrb7iEjqZ__Mtq_kuNsU'; //姜 
	$apple[1] = 'o3eaus026_GoLytvNkSQIrRRdtOs'; //汪
	$apple[2] = 'o3eaus40d4iMREKa4pvHo1ZfaW88'; //阳
	$apple[3] = 'o3eaus0YpPcM62Xy15BvvFS-B4do'; //菲
	$apple[4] = 'o3eaus8lUvK0eiWB2n4iU1TAdeDs';  //陈
	$apple[5] = 'o3eaus94pYr512rfPxEoodSwz52Y'; //汪
	if( ( ( $h >='06' ) && ( $h <= '23' ) ) || ( $h == '00'  )   )
	{	
	    $in_apple = in_array( $openID , $apple );
	    if( !$in_apple )
	    {	
	    	if( ($relation =='儿子' ) || ( $relation =='女儿' ) || ( $relation =='孙子' ) || ( $relation == '孙女' ) )
	    	{
		   // if( in_array( $sub_did , $arr ) ) 
		    {
			DB::table( 'vol_0601' )->insert( [ 'did'=>$did ,'time'=>date('Y-m-d H:i:s' , $now)] );
                 	 $url = "http://www.laoyou1.me/chiyao";
                    	 $this->https2_request( $url , [ 'did'=>$did,'line'=>'0601' ] );
       		    }
	    	}
	    }
	}

    }
    public function https2_request($url,$data = null){
    	$curl = curl_init();
    	curl_setopt($curl, CURLOPT_URL, $url);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    	if (!empty($data)){
        	curl_setopt($curl, CURLOPT_POST, 1);
        	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
   	 }
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	$output = curl_exec($curl);
    	curl_close($curl);
    	return $output;

    }

}
